package com.bit.forum.controller;


import com.bit.forum.common.AppResult;
import com.bit.forum.common.ResultCode;
import com.bit.forum.config.AppConfig;
import com.bit.forum.model.Article;
import com.bit.forum.model.Board;
import com.bit.forum.model.User;
import com.bit.forum.service.IArticleService;
import com.bit.forum.service.IBoardService;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpSession;
import lombok.NonNull;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/article")
public class ArticleController {
    @Resource
    private IBoardService boardService;
    @Resource
    private IArticleService articleService;

    @PostMapping("/create")
    public AppResult create(HttpServletRequest request,
                            @RequestParam("boardId") @NonNull Long boardId,
                            @RequestParam("title") @NonNull String title,
                            @RequestParam("content") @NonNull String content){
        //校验用户是否禁言
        HttpSession session = request.getSession(false);
        User user =(User) session.getAttribute(AppConfig.USER_SESSION);
        if(user == null){
            log.info(ResultCode.FAILED_USER_NOT_EXISTS.toString());
            return AppResult.failed(ResultCode.FAILED_USER_NOT_EXISTS);
        }
        if(user.getState()==1){
            return AppResult.failed(ResultCode.FAILED_USER_BANNED);
        }
        //板块校验
        Board board=boardService.selectById(boardId);
        if(board==null||board.getState()==1||board.getDeleteState()==1){
            log.warn(ResultCode.ERROR_BOARD_NOT_EXISTS.toString());
            return AppResult.failed(ResultCode.ERROR_BOARD_NOT_EXISTS);
        }
        //封装文章对象
        Article article=new Article();
        article.setTitle(title);//标题
        article.setContent(content);//正文
        article.setBoardId(boardId);//板块
        article.setUserId(user.getId());//作者
        articleService.create(article);
        //响应
        return AppResult.success();
    }
    //获取帖子列表
    @GetMapping("/getAllByBoradId")
    public AppResult<List<Article>> getAllByBoradId(@RequestParam(value = "boardId",required = false) Long boardId){
        List<Article> articles;
        if(boardId==null){
            articles=articleService.selectAll();
        }else {
            articles=articleService.selectAllByBoardId(boardId);
        }
        if(articles==null){
            articles=new ArrayList<>();
        }
        return AppResult.success(articles);
    }
    //获取帖子详情
    @GetMapping("/getDetailById")
    public AppResult<Article> getDetailById(HttpServletRequest request,
            @RequestParam("articleId") @NonNull Long articleId){
        HttpSession session = request.getSession(false);
        User user =(User) session.getAttribute(AppConfig.USER_SESSION);
        Article article = articleService.selectDetailById(articleId);
        if (article==null){
            log.warn(ResultCode.FAILED_ARTICLE_NOT_EXISTS.toString());
            return AppResult.failed(ResultCode.FAILED_ARTICLE_NOT_EXISTS);
        }
        //判断是否为作者本人
        if(user.getId()==article.getUserId()){
            article.setOwn(true);
        }
        return AppResult.success(article);
    }
    //修改帖子
    @PostMapping("/modify")
    public AppResult modify(HttpServletRequest request,
                            @RequestParam("id") @NonNull Long id,
                            @RequestParam("title") @NonNull String title,
                            @RequestParam("content") @NonNull String content){
        HttpSession session = request.getSession(false);
        User user=(User) session.getAttribute((AppConfig.USER_SESSION));
        if(user==null||user.getDeleteState()==1){
            return AppResult.failed(ResultCode.FAILED_USER_NOT_EXISTS);
        }
        Article article=articleService.selectDetailById(id);
        if(article==null){
            log.warn(ResultCode.FAILED_ARTICLE_NOT_EXISTS.toString());
            return AppResult.failed(ResultCode.FAILED_ARTICLE_NOT_EXISTS);
        }
        //判断是否为作者本人
        if(user.getId()!=article.getUserId()){
            return AppResult.failed(ResultCode.FAILED_USER_NOT_EXISTS);
        }
        //判断帖子状态
        if(article.getDeleteState()==1||article.getState()==1){
            return AppResult.failed(ResultCode.FAILED_ARTICLE_NOT_EXISTS);
        }
        articleService.modify(id, title, content);
        // 打印日志
        log.info("帖子更新成功. Article id = " + id + "User id = " + user.getId() + ".");
        return AppResult.success();
    }

    @GetMapping("/thumbsUp")
    public AppResult thumbsUp(HttpServletRequest request,
                             @RequestParam("id") @NonNull Long id){
        if(id==null){
            log.warn(ResultCode.FAILED_ARTICLE_NOT_EXISTS.toString());
            return AppResult.failed(ResultCode.FAILED_ARTICLE_NOT_EXISTS);
        }
        HttpSession session = request.getSession(false);
        User user =(User) session.getAttribute(AppConfig.USER_SESSION);
        if(user==null||user.getDeleteState()==1){
            return AppResult.failed(ResultCode.FAILED_USER_NOT_EXISTS);
        }
        if(user.getState()==1){
            return AppResult.failed(ResultCode.FAILED_USER_BANNED);
        }
        articleService.thumbsUpById(id);
        return AppResult.success();
    }

    @PostMapping("delete")
    public AppResult deleteById(HttpServletRequest request,
                               @RequestParam("id") @NonNull Long id){
        HttpSession session = request.getSession(false);
        User user =(User) session.getAttribute(AppConfig.USER_SESSION);
        if(user==null||user.getDeleteState()==1){
            return AppResult.failed(ResultCode.FAILED_USER_NOT_EXISTS);
        }
        Article article=articleService.selectById(id);
        // 校验帖子状态
        if (article == null || article.getDeleteState() == 1) {
            // 帖子已删除
            return AppResult.failed(ResultCode.FAILED_ARTICLE_NOT_EXISTS);
        }
        // 校验当前登录的用户是不是作者
        if (user.getId() != article.getUserId()) {
            return AppResult.failed(ResultCode.FAILED_FORBIDDEN);
        }
        // 调用Service
        articleService.deleteById(id);
        // 返回操作成功
        return AppResult.success();
    }
}
